unit uDosCMD;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, ToolWin, ComCtrls, DB, IBQuery, ActnList,
  System.Actions, Vcl.Buttons, dateutils;

type
  TDosCMD_F = class(TForm)
    Info_Pesquisa: TToolBar;
    AcDosCMD: TActionList;
    Sair: TAction;
    OK: TAction;
    Memo1: TMemo;
    tbTOP: TToolBar;
    lblMensagem: TLabel;
    tbTopDesc: TToolBar;
    Info_Command: TLabel;
    Action1: TAction;
    spGrava: TSpeedButton;
    spSair: TSpeedButton;
    procedure GridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure SairExecute(Sender: TObject);
    procedure OKExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private


    { Private declarations }
  public
    function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;

    { Public declarations }
  end;

var
  DosCMD_F: TDosCMD_F;

implementation

uses
  mdsrv_u, uFunctions;

{$R *.dfm}



procedure TDosCMD_F.FormClose(Sender: TObject; var Action: TCloseAction);
var
i:SmallInt;
begin

   for I := 0 to Memo1.Lines.Count-1 do
     AddLog(FormatDateTime('dd/mm/yyyy',Today)+' - '+ Memo1.Lines[i],'DosCmd_log.txt' );



   Action := CaFree;

end;

function TDosCMD_F.GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;

var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir: string;
  Handle: Boolean;
begin
  //Memo1.Lines.Add('C>'+ CommandLine);
  Info_Command.Caption:= 'Executando: '+ CommandLine;
  Result := '';
  //Result := 'C>'+ CommandLine;
  with SA do begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C' + CommandLine),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            Buffer[BytesRead] := #0;

            if pos(', #8',Buffer)>0 then
              Result := Buscar_Trocar(Buffer,', #8','')
            else
              Result := Buffer;

            if pos(''#8#8#8#8'',Result)>0 then
              Begin

                Result:=Buscar_Trocar(Result,'     ','');
                Result:=Buscar_Trocar(Result,'','')
              End;

            if trim(Result)<>'' then
              Memo1.Lines.Text := Memo1.Lines.Text + Result;
//            Memo1.Lines.Text := Memo1.Lines.Text + Buffer;
//               Result := Result + Buffer;

            Memo1.SelLength := 0;
            Memo1.SelStart:= Memo1.GetTextLen;
            Memo1.Perform( EM_SCROLLCARET, 0, 0 );
            Application.ProcessMessages;
            Sleep(50);

          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally

        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally

    CloseHandle(StdOutPipeRead);
    Info_Command.Caption:= 'Executado: C>'+ CommandLine;
  end;


end;





procedure TDosCMD_F.GridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin


  if key = vk_return then
    Begin

    End;
end;

procedure TDosCMD_F.SairExecute(Sender: TObject);
begin
  Self.ModalResult := mrCancel;

end;

procedure TDosCMD_F.OKExecute(Sender: TObject);
begin
   Memo1.Lines.Text := PegarSaidaDOS('s:\unrar.exe x C:\SupportTI\version.rar /y','C:\');

  Self.ModalResult := mrOK;
end;




end.
